<?php
namespace app\datamanage\controller;
use think\Controller;
use think\Db;

/**
 *
 */
class Pinpai extends Logincheck
{

    function showPinpai($teacase='',$teaarea='')
    {
        if($teacase==''&&$teaarea==''){
            //返回茶类
            $retval = getTeaCase();
            $this->assign('teacase', $retval);
            $this->assign('data', '');
            $this->assign("currentteacase", $teacase);
            $this->assign("currentteaarea", $teaarea);
            $this->assign('areas','');
            return $this->fetch();
        }

        //返回茶类
        $retval = getTeaCase();
        $this->assign('teacase', $retval);
        //根据传参选择返回数据
        if($teacase==10000){
            $retval = model("Pinpai")->showPinpai();
        }else if($teacase==9999){
            $condition = ['bangdan_id'=>4];
            $retval = model("Pinpai")->showPinpai($condition);
        }else{
            if($teaarea==9999){
                $condition = ['teacase_id'=>$teacase, 'bangdan_id'=>4];
                $retval = model("Pinpai")->showPinpai($condition);
            }else if($teaarea==10000){
                $condition = ['teacase_id'=>$teacase];
                $retval = model("Pinpai")->showPinpai($condition);
            }else{
                $condition = ['teacase_id'=>$teacase, 'teaarea_id'=>$teaarea];
                $retval = model("Pinpai")->showPinpai($condition);
            }
        }

        $tid_name = teacase_ids();
        $aid_name = teaarea_ids();
        $rank = 1;
        foreach ($retval as &$ret) {
            $ret['rank'] = $rank;
            $rank++;
            $ret['teacase'] = $tid_name[$ret['teacase_id']];
            $ret['teaarea'] = $aid_name[$ret['teaarea_id']];
            // 成立年限
            //$ret['established'] = $this ->established($ret['detailintro']);
            $established = $this ->established($ret['detailintro']);

            //零售额
            //$ret['sale_point'] = $this ->sale($ret['sale']);
            $sale_point = $this ->sale($ret['sale']);

            //页面访问(未完成)

            //茶人传
            $biography = $this->biography($ret['biography']);

            //微信公众号
            //$ret['pnumber_point'] = $this ->pnumber($ret['pnumber']);
            $pnumber_point = $this ->pnumber($ret['pnumber']);

            // 访问量得分
            $click_point = $ret['click']*10;

            //随机数
            $ret['random_point'] = rand(1,30)*10;

            $ret['total_point'] = $established + $sale_point + $pnumber_point + $biography + $click_point;

        }
        // print_pre($retval);
        $this->assign("currentteacase", $teacase);
        $this->assign("currentteaarea", $teaarea);
        $this->assign('data',$retval);

        $retval = $this->reTeaArea($teacase);
        $this->assign('areas',$retval);
        return $this->fetch();
    }

    //同步数据
    function updateAll()
    {
        $retval = model("Pinpai")->showPinpai();
        foreach ($retval as $ret) {
            // 成立年限
            // $ret['established'] = $this ->established($ret['detailintro']);
            $established = $this ->established($ret['detailintro']);

            //零售额
            // $ret['sale_point'] = $this ->sale($ret['sale']);
            $sale_point = $this ->sale($ret['sale']);

            //页面访问(未完成)

            //茶人传
            $biography_point = $this->biography($ret['biography']);

            //微信公众号
            // $ret['pnumber_point'] = $this ->pnumber($ret['pnumber']);
            $pnumber_point = $this ->pnumber($ret['pnumber']);

            $total_point = $established + $sale_point + $pnumber_point + $biography_point ;

            model('Pinpai')->updateAll($ret['id'], $total_point);
        }
        return $this->redirect('showPinpai');
    }

    function modify($id, $newscore){
        model('Pinpai')->updateAll($id, $newscore);
        return $newscore;
    }


    //返回某茶类对应的产地JSON数据
    function reTeaAreaJson($teacase_id)
    {
        // $data = $this->reTeaArea($teacase_id);
        $data = $this->reTeaArea($teacase_id);
        return json_encode($data);
    }


    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
        $retval = model("Jren")->reTeaArea($teacase_id);
        $areas = getTeaArea();
        $data = [];
        foreach ($retval as $ret) {
            foreach ($areas as $area) {
                if($ret["teaarea_id"]==$area["id"]){
                    $data[] = $area;
               }
           }
       }
       return $data;
    }

    function havebiography($id, $score, $biography)
    {
        $data = ['score'=>$score, 'biography'=>$biography];
        db('pinpai')->where('id', $id)->update($data);
        return 1;
    }

/*************************************************************************************************/
    //获取年限分数
    function established($detailintro)
    {
        $cyear = 0;
        $point = 0;
        $pattern = '/成立于(\d{4})/';
        preg_match_all($pattern, $detailintro, $year);
         // print_pre($year);
        if($year[1]){
            $now = date("Y");
            $cyear = $now - $year[1][0];
        }else{
            $pattern = '/(\d{4})年/';
            preg_match_all($pattern, $detailintro, $year);
            if($year[1]){
                $now = date("Y");
                $cyear = $now - $year[1][0];
                if($cyear<0){
                    $cyear = 0;
                }
            }
        }

        if($cyear <= 5){
            $point = $cyear * 200;
        }else if($cyear<=10){
            $point = 1000 + ($cyear-5) * 300;
        }else if($cyear<=15){
            $point = 2500 + ($cyear-10) * 500;
        }else if($cyear<=20){
            $point = 5000 + ($cyear-15) * 800;
        }else{
            $point = 9000;
        }
        return $point;
    }

    //零售额
    function sale($sale)
    {
        $sale_point = 0;
        $pattern = '/100万/';
        if(preg_match($pattern, $sale)){
            $sale_point = 100;
            return $sale_point;
        }

        $pattern = '/100~300万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 250;
            return $sale_point;
        }

        $pattern = '/300~1000万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 500;
            return $sale_point;
        }

        $pattern = '/1000~3000万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 800;
            return $sale_point;
        }

        $pattern = '/3000~10000万元/';
        if(preg_match($pattern, $sale)){
            $sale_point = 1500;
            return $sale_point;
        }

        $pattern = '/1~3亿/';
        if(preg_match($pattern, $sale)){
            $sale_point = 2000;
            return $sale_point;
        }

        $pattern = '/3亿/';
        if(preg_match($pattern, $sale)){
            $sale_point = 3000;
            return $sale_point;
        }
        return $sale_point ;
    }

    //公众号得分
    function pnumber($pnumber)
    {
        if($pnumber){
            $pnumber_point = 300;

        }else{
            $pnumber_point = 0;
        }
        return $pnumber_point;
    }

    //茶人传
    function biography($biography)
    {
        $point = 0;
        if($biography==1){
            $point = 10000;
        }
        return $point;
    }















}

?>